WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
SE3.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21#include "core/common/wolf.h"
22#include "core/math/rotations.h"
24
25/*
26 * The functions in this file are related to manipulations of Delta motion magnitudes used in 3d motion.
27 *
28 * The Delta is defined as a simple 3d pose with the rotation expressed in quaternion form,
29 * Delta = [Dp, Dq]
30 * with
31 * Dp : position delta
32 * Dq : quaternion delta
33 * and its tangent vector (Lie algebra) is tau = [rho, theta].
34 *
35 * The functions are listed below:
36 *
37 * - identity: I = Delta at the origin, with Dp = [0,0,0]; Dq = [0,0,0,1], so that D (+) I = I (+) D = D
38 * - inverse: so that D (+) D.inv = D.inv (+) D = I
39 * - compose: Dc = D1 (+) D2
40 * - between: Db = D2 (-) D1 := D1.inv (+) D2, so that D2 = D1 (+) Db
41 * - exp: go from tangent space to delta manifold (SE(3) exponential map)
42 * - log: go from delta manifold to tangent space (SE(3) logarithmic map)
43 * - plus: block-wise: D2 = [Dp1 + dp, Dq1 * exp_q(do)]
44 * - minus: block-wise: d = [Dp2 - Dp1, log_q(Dq1.conj * Dq2)], so that D2 = plus(D1, d)
45 * - interpolate: D = D1 (+) exp( t * log( D1.inv (+) D2 ) )
46 * - toSE3: 4x4 homogeneous matrix
47 * - jac_SE3_left, jac_SE3_right, jac_SE3_left_inv, jac_SE3_right_inv: Jacobians of SE(3)
48 *
49 * Each function is provided (when applicable) with the following APIs:
50 * - by parts, with the rotation as a quaternion (Eigen::QuaternionBase)
51 * - by parts, with the rotation as a vector (quaternion coefficients [x, y, z, w])
52 * - full 7-vector [p, q] as output parameter and as return value
53 * - full 7-vector with Jacobians. Jacobians are block-wise: w.r.t. perturbations [dp, do] with additive dp
54 * and local (right) do on the quaternion, i.e. consistent with plus() and minus().
55 * - VectorComposite with keys 'P' and 'O', as output parameter and as return value
56 *
57 * ------------------------------------------------------------------------------------------
58 *
59 * Some of the functions below are based on:
60 *
61 * [1] J. Sola et. al. "A micro Lie theory for state estimation in robotics", tech rep. IRI 2018,
62 * https://arxiv.org/pdf/1812.01537.pdf
63 */
64
65namespace wolf
66{
67namespace SE3
68{
69using namespace Eigen;
70
72// JACOBIANS OF SE(3)
73
84template <typename D1, typename D2>
85inline Matrix<typename D1::Scalar, 3, 3> Q_helper(const MatrixBase<D1>& v, const MatrixBase<D2>& w)
86{
87 typedef typename D1::Scalar T;
88
89 Matrix<T, 3, 3> V = skew(v);
90 Matrix<T, 3, 3> W = skew(w);
91 Matrix<T, 3, 3> WV = W * V, VW = V * W, WW = W * W;
92
93 T th_2 = w.squaredNorm();
94 T c2, c3, c4;
95
96 if (th_2 < T(1e-4)) // th < 1e-2
97 {
98 T th_4 = th_2 * th_2;
99 c2 = T(1. / 6.) - th_2 / T(120.) + th_4 / T(5040.);
100 c3 = T(1. / 24.) - th_2 / T(720.) + th_4 / T(40320.);
101 c4 = T(1. / 120.) - th_2 / T(2520.) + th_4 / T(120960.);
102 }
103 else
104 {
105 T th = sqrt(th_2);
106 T sin_th = sin(th);
107 T cos_th = cos(th);
108 c2 = (th - sin_th) / (th_2 * th);
109 c3 = (th_2 + T(2.0) * cos_th - T(2.0)) / (T(2.0) * th_2 * th_2);
110 c4 = (T(2.0) * th - T(3.0) * sin_th + th * cos_th) / (T(2.0) * th_2 * th_2 * th);
111 }
112
113 Matrix<T, 3, 3> WVW = WV * W;
114 Matrix<T, 3, 3> Q =
115 T(0.5) * V + c2 * (WV + VW + WVW) + c3 * (WW * V + VW * W - T(3.0) * WVW) + c4 * (WVW * W + WW * V * W);
116 return Q;
117}
118
121template <typename D>
122inline Matrix<typename D::Scalar, 6, 6> jac_SE3_left(const MatrixBase<D>& tangent)
123{
125 typedef typename D::Scalar T;
126
127 Matrix<T, 3, 1> v = tangent.template head<3>(); // linear
128 Matrix<T, 3, 1> w = tangent.template tail<3>(); // angular
129
130 Matrix<T, 3, 3> Jl = jac_SO3_left(w);
131
132 Matrix<T, 6, 6> Jl_SE3;
133 Jl_SE3.topLeftCorner(3, 3) = Jl;
134 Jl_SE3.bottomRightCorner(3, 3) = Jl;
135 Jl_SE3.topRightCorner(3, 3) = Q_helper(v, w);
136 Jl_SE3.bottomLeftCorner(3, 3).setZero();
137 return Jl_SE3;
138}
139
142template <typename D>
143inline Matrix<typename D::Scalar, 6, 6> jac_SE3_right(const MatrixBase<D>& tangent)
144{
145 return jac_SE3_left(Matrix<typename D::Scalar, 6, 1>(-tangent));
146}
147
150template <typename D>
151inline Matrix<typename D::Scalar, 6, 6> jac_SE3_left_inv(const MatrixBase<D>& tangent)
152{
154 typedef typename D::Scalar T;
155
156 Matrix<T, 3, 1> v = tangent.template head<3>(); // linear
157 Matrix<T, 3, 1> w = tangent.template tail<3>(); // angular
158
159 Matrix<T, 3, 3> Jl_inv = jac_SO3_left_inv(w);
160
161 Matrix<T, 6, 6> Jl_inv_SE3;
162 Jl_inv_SE3.topLeftCorner(3, 3) = Jl_inv;
163 Jl_inv_SE3.bottomRightCorner(3, 3) = Jl_inv;
164 Jl_inv_SE3.topRightCorner(3, 3) = -Jl_inv * Q_helper(v, w) * Jl_inv;
165 Jl_inv_SE3.bottomLeftCorner(3, 3).setZero();
166 return Jl_inv_SE3;
167}
168
171template <typename D>
172inline Matrix<typename D::Scalar, 6, 6> jac_SE3_right_inv(const MatrixBase<D>& tangent)
173{
174 return jac_SE3_left_inv(Matrix<typename D::Scalar, 6, 1>(-tangent));
175}
176
178// IDENTITY
179
182template <typename D1, typename D2>
183inline void identity(MatrixBase<D1>& p, QuaternionBase<D2>& q)
184{
186 p.setZero();
187 q.setIdentity();
188}
189
192template <typename D1, typename D2>
193inline void identity(MatrixBase<D1>& p, MatrixBase<D2>& q)
194{
197
198 Map<Quaternion<typename D2::Scalar>> mq(&q(0));
199 identity(p, mq);
200}
201
204template <typename D>
205inline void identity(MatrixBase<D>& d)
206{
208
209 Map<Matrix<typename D::Scalar, 3, 1>> p(&d(0));
210 Map<Quaternion<typename D::Scalar>> q(&d(3));
211 identity(p, q);
212}
213
216template <typename T = double>
217inline Matrix<T, 7, 1> identity()
218{
219 Matrix<T, 7, 1> ret;
220 identity(ret);
221 return ret;
222}
223
227{
228 v.clear();
229 v['P'].resize(3);
230 v['O'].resize(4);
231 identity(v['P'], v['O']);
232}
233
237{
239 identity(v);
240 return v;
241}
242
244// INVERSE
245
248template <typename D1, typename D2, typename D4, typename D5>
249inline void inverse(const MatrixBase<D1>& p, const QuaternionBase<D2>& q, MatrixBase<D4>& ip, QuaternionBase<D5>& iq)
250{
253
254 ip = -(q.conjugate() * p);
255 iq = q.conjugate();
256}
257
260template <typename D1, typename D2, typename D4, typename D5>
261inline void inverse(const MatrixBase<D1>& p, const MatrixBase<D2>& q, MatrixBase<D4>& ip, MatrixBase<D5>& iq)
262{
265
266 Map<const Quaternion<typename D2::Scalar>> mq(&q(0));
267 Map<Quaternion<typename D5::Scalar>> miq(&iq(0));
268
269 inverse(p, mq, ip, miq);
270}
271
274template <typename D1, typename D2>
275inline void inverse(const MatrixBase<D1>& d, MatrixBase<D2>& id)
276{
279
280 Map<const Matrix<typename D1::Scalar, 3, 1>> p(&d(0));
281 Map<const Quaternion<typename D1::Scalar>> q(&d(3));
282 Map<Matrix<typename D2::Scalar, 3, 1>> ip(&id(0));
283 Map<Quaternion<typename D2::Scalar>> iq(&id(3));
284
285 inverse(p, q, ip, iq);
286}
287
290template <typename D>
291inline Matrix<typename D::Scalar, 7, 1> inverse(const MatrixBase<D>& d)
292{
293 Matrix<typename D::Scalar, 7, 1> id;
294 inverse(d, id);
295 return id;
296}
297
300inline void inverse(const VectorComposite& v, VectorComposite& c)
301{
302 c['P'].resize(3);
303 c['O'].resize(4);
304 inverse(v.at('P'), v.at('O'), c['P'], c['O']);
305}
306
310{
312 inverse(v, c);
313 return c;
314}
315
317// COMPOSE
318
321template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
322inline void compose(const MatrixBase<D1>& p1,
323 const QuaternionBase<D2>& q1,
324 const MatrixBase<D4>& p2,
325 const QuaternionBase<D5>& q2,
326 MatrixBase<D7>& pc,
327 QuaternionBase<D8>& qc)
328{
332
333 pc = p1 + q1 * p2;
334 qc = q1 * q2; // q here to avoid possible aliasing between d1 and sum
335}
336
339template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
340inline void compose(const MatrixBase<D1>& p1,
341 const MatrixBase<D2>& q1,
342 const MatrixBase<D4>& p2,
343 const MatrixBase<D5>& q2,
344 MatrixBase<D7>& pc,
345 MatrixBase<D8>& qc)
346{
350
351 Map<const Quaternion<typename D2::Scalar>> mq1(&q1(0));
352 Map<const Quaternion<typename D5::Scalar>> mq2(&q2(0));
353 Map<Quaternion<typename D8::Scalar>> mqc(&qc(0));
354
355 compose(p1, mq1, p2, mq2, pc, mqc);
356}
357
360template <typename D1, typename D2, typename D3>
361inline void compose(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2, MatrixBase<D3>& sum)
362{
366
367 Map<const Matrix<typename D1::Scalar, 3, 1>> p1(&d1(0));
368 Map<const Quaternion<typename D1::Scalar>> q1(&d1(3));
369 Map<const Matrix<typename D2::Scalar, 3, 1>> p2(&d2(0));
370 Map<const Quaternion<typename D2::Scalar>> q2(&d2(3));
371 Map<Matrix<typename D3::Scalar, 3, 1>> pc(&sum(0));
372 Map<Quaternion<typename D3::Scalar>> qc(&sum(3));
373
374 compose(p1, q1, p2, q2, pc, qc);
375}
376
379template <typename D1, typename D2>
380inline Matrix<typename D1::Scalar, 7, 1> compose(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2)
381{
382 Matrix<typename D1::Scalar, 7, 1> ret;
383 compose(d1, d2, ret);
384 return ret;
385}
386
389template <typename D1, typename D2, typename D3, typename D4, typename D5>
390inline void compose(const MatrixBase<D1>& d1,
391 const MatrixBase<D2>& d2,
392 MatrixBase<D3>& sum,
393 MatrixBase<D4>& J_sum_d1,
394 MatrixBase<D5>& J_sum_d2)
395{
401
402 // Some useful temporaries
403 Matrix<typename D1::Scalar, 3, 3> dR1 = q2R(d1.segment(3, 4)); // q1.matrix(); // First Delta, DR
404 Matrix<typename D2::Scalar, 3, 3> dR2 = q2R(d2.segment(3, 4)); // q2.matrix(); // Second delta, dR
405
406 // Jac wrt first delta
407 J_sum_d1.setIdentity(); // dDp'/dDp = dDv'/dDv = I
408 J_sum_d1.block(0, 3, 3, 3).noalias() = -dR1 * skew(d2.head(3)); // dDp'/dDo
409 J_sum_d1.block(3, 3, 3, 3) = dR2.transpose(); // dDo'/dDo
410
411 // Jac wrt second delta
412 J_sum_d2.setIdentity(); //
413 J_sum_d2.block(0, 0, 3, 3) = dR1; // dDp'/p
414 // J_sum_d2.block(3,3,3,3) = Matrix3d::Identity(); // dDo'/ddo = I
415
416 // compose deltas -- done here to avoid aliasing when calling with input `d1` and result `sum` referencing the same
417 // variable
418 compose(d1, d2, sum);
419}
420
423inline void compose(const VectorComposite& x1, const VectorComposite& x2, VectorComposite& c)
424{
425 c['P'].resize(3);
426 c['O'].resize(4);
427 compose(x1.at('P'), x1.at('O'), x2.at('P'), x2.at('O'), c['P'], c['O']);
428}
429
433{
435 compose(x1, x2, c);
436 return c;
437}
438
439// inline void compose(const VectorComposite& x1,
440// const VectorComposite& x2,
441// VectorComposite& c,
442// MatrixComposite& J_c_x1,
443// MatrixComposite& J_c_x2)
444// {
445// // Some useful temporaries
446// const auto R1 = q2R(x1.at('O')); // q1.matrix(); // make temporary
447// const auto& R2 = q2R(x2.at('O')); // q2.matrix(); // do not make temporary, only reference
448
449// // Jac wrt first delta
450// J_c_x1.emplace('P', 'P', Matrix3d::Identity());
451// J_c_x1.emplace('P', 'O', -R1 * skew(x2.at('P')));
452// J_c_x1.emplace('O', 'P', Matrix3d::Zero());
453// J_c_x1.emplace('O', 'O', R2.transpose());
454
455// // Jac wrt second delta
456// J_c_x2.emplace('P', 'P', R1);
457// J_c_x2.emplace('P', 'O', Matrix3d::Zero());
458// J_c_x2.emplace('O', 'P', Matrix3d::Zero());
459// J_c_x2.emplace('O', 'O', Matrix3d::Identity());
460
461// // compose deltas -- done here to avoid aliasing when calling with input `x1` and result `c` referencing the
462// same
463// // variable
464// compose(x1, x2, c);
465// }
466
468// BETWEEN
469
472template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
473inline void between(const MatrixBase<D1>& p1,
474 const QuaternionBase<D2>& q1,
475 const MatrixBase<D4>& p2,
476 const QuaternionBase<D5>& q2,
477 MatrixBase<D7>& p12,
478 QuaternionBase<D8>& q12)
479{
483
484 p12 = q1.conjugate() * (p2 - p1);
485 q12 = q1.conjugate() * q2;
486}
487
490template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
491inline void between(const MatrixBase<D1>& p1,
492 const MatrixBase<D2>& q1,
493 const MatrixBase<D4>& p2,
494 const MatrixBase<D5>& q2,
495 MatrixBase<D7>& p12,
496 MatrixBase<D8>& q12)
497{
501
502 Map<const Quaternion<typename D2::Scalar>> mq1(&q1(0));
503 Map<const Quaternion<typename D5::Scalar>> mq2(&q2(0));
504 Map<Quaternion<typename D8::Scalar>> mq12(&q12(0));
505
506 between(p1, mq1, p2, mq2, p12, mq12);
507}
508
511template <typename D1, typename D2, typename D3>
512inline void between(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2, MatrixBase<D3>& d12)
513{
517
518 Map<const Matrix<typename D1::Scalar, 3, 1>> p1(&d1(0));
519 Map<const Quaternion<typename D1::Scalar>> q1(&d1(3));
520 Map<const Matrix<typename D2::Scalar, 3, 1>> p2(&d2(0));
521 Map<const Quaternion<typename D2::Scalar>> q2(&d2(3));
522 Map<Matrix<typename D3::Scalar, 3, 1>> p12(&d12(0));
523 Map<Quaternion<typename D3::Scalar>> q12(&d12(3));
524
525 between(p1, q1, p2, q2, p12, q12);
526}
527
530template <typename D1, typename D2>
531inline Matrix<typename D1::Scalar, 7, 1> between(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2)
532{
533 Matrix<typename D1::Scalar, 7, 1> d12;
534 between(d1, d2, d12);
535 return d12;
536}
537
540inline void between(const VectorComposite& x1, const VectorComposite& x2, VectorComposite& x12)
541{
542 x12['P'].resize(3);
543 x12['O'].resize(4);
544 between(x1.at('P'), x1.at('O'), x2.at('P'), x2.at('O'), x12['P'], x12['O']);
545}
546
550{
551 VectorComposite x12;
552 between(x1, x2, x12);
553 return x12;
554}
555
557// EXP
558
562template <typename D1, typename D2, typename D3, typename D4>
563inline void exp(const MatrixBase<D1>& rho, const MatrixBase<D2>& theta, MatrixBase<D3>& p, QuaternionBase<D4>& q)
564{
568
569 // see [1] eqs. (174) and (145)
570 p = jac_SO3_left(theta) * rho;
571 q = exp_q(theta);
572}
573
577template <typename D1, typename D2, typename D3, typename D4>
578inline void exp(const MatrixBase<D1>& rho, const MatrixBase<D2>& theta, MatrixBase<D3>& p, MatrixBase<D4>& q)
579{
581
582 Map<Quaternion<typename D4::Scalar>> mq(&q(0));
583 exp(rho, theta, p, mq);
584}
585
588template <typename D1, typename D2>
589inline void exp(const MatrixBase<D1>& tau, MatrixBase<D2>& delta)
590{
593
594 Map<const Matrix<typename D1::Scalar, 3, 1>> rho(&tau(0));
595 Map<const Matrix<typename D1::Scalar, 3, 1>> theta(&tau(3));
596 Map<Matrix<typename D2::Scalar, 3, 1>> p(&delta(0));
597 Map<Quaternion<typename D2::Scalar>> q(&delta(3));
598
599 exp(rho, theta, p, q);
600}
601
604template <typename D>
605inline Matrix<typename D::Scalar, 7, 1> exp(const MatrixBase<D>& tau)
606{
607 Matrix<typename D::Scalar, 7, 1> delta;
608 exp(tau, delta);
609 return delta;
610}
611
614template <typename D1, typename D2, typename D3>
615inline void exp(const MatrixBase<D1>& tau, MatrixBase<D2>& delta, MatrixBase<D3>& J_delta_tau)
616{
619 MatrixSizeCheck<6, 6>::check(J_delta_tau);
620
621 typedef typename D1::Scalar T;
622
623 Matrix<T, 6, 1> tau_tmp = tau; // copy (tau and delta may alias)
624 Matrix<T, 3, 1> rho = tau_tmp.template head<3>();
625 Matrix<T, 3, 1> theta = tau_tmp.template tail<3>();
626 Matrix<T, 3, 3> Jl = jac_SO3_left(theta);
627 Matrix<T, 3, 1> p = Jl * rho;
628
629 /* Block-wise jacobian (additive position, local rotation), from the left Jacobian of SE(3) [Jl, Q; 0, Jl]:
630 * J_p_rho = Jl(theta)
631 * J_p_theta = Q(rho, theta) - [p]x * Jl(theta)
632 * J_o_rho = 0
633 * J_o_theta = Jr(theta)
634 */
635 J_delta_tau.setZero();
636 J_delta_tau.template topLeftCorner<3, 3>() = Jl;
637 J_delta_tau.template topRightCorner<3, 3>() = Q_helper(rho, theta) - skew(p) * Jl;
638 J_delta_tau.template bottomRightCorner<3, 3>() = jac_SO3_right(theta);
639
640 exp(tau_tmp, delta);
641}
642
645inline void exp(const VectorComposite& tau, VectorComposite& delta)
646{
647 // temporaries: tangent and manifold 'O' have different sizes (tau and delta may be the same object)
648 Vector3d p;
649 Vector4d q;
650 exp(tau.at('P'), tau.at('O'), p, q);
651 delta['P'] = p;
652 delta['O'] = q;
653}
654
658{
659 VectorComposite delta;
660 exp(tau, delta);
661 return delta;
662}
663
665// LOG
666
670template <typename D1, typename D2, typename D3, typename D4>
671inline void log(const MatrixBase<D1>& p, const QuaternionBase<D2>& q, MatrixBase<D3>& rho, MatrixBase<D4>& theta)
672{
676
677 // see [1] eqs. (173) and (146)
678 Matrix<typename D2::Scalar, 3, 1> th = log_q(q);
679 rho = jac_SO3_left_inv(th) * p;
680 theta = th;
681}
682
686template <typename D1, typename D2, typename D3, typename D4>
687inline void log(const MatrixBase<D1>& p, const MatrixBase<D2>& q, MatrixBase<D3>& rho, MatrixBase<D4>& theta)
688{
690
691 Map<const Quaternion<typename D2::Scalar>> mq(&q(0));
692 log(p, mq, rho, theta);
693}
694
697template <typename D1, typename D2>
698inline void log(const MatrixBase<D1>& delta, MatrixBase<D2>& tau)
699{
702
703 Map<const Matrix<typename D1::Scalar, 3, 1>> p(&delta(0));
704 Map<const Quaternion<typename D1::Scalar>> q(&delta(3));
705 Map<Matrix<typename D2::Scalar, 3, 1>> rho(&tau(0));
706 Map<Matrix<typename D2::Scalar, 3, 1>> theta(&tau(3));
707
708 log(p, q, rho, theta);
709}
710
713template <typename D>
714inline Matrix<typename D::Scalar, 6, 1> log(const MatrixBase<D>& delta)
715{
716 Matrix<typename D::Scalar, 6, 1> tau;
717 log(delta, tau);
718 return tau;
719}
720
723template <typename D1, typename D2, typename D3>
724inline void log(const MatrixBase<D1>& delta, MatrixBase<D2>& tau, MatrixBase<D3>& J_tau_delta)
725{
728 MatrixSizeCheck<6, 6>::check(J_tau_delta);
729
730 typedef typename D1::Scalar T;
731
732 Matrix<T, 6, 1> tau_tmp;
733 log(delta, tau_tmp);
734 Matrix<T, 3, 1> rho = tau_tmp.template head<3>();
735 Matrix<T, 3, 1> theta = tau_tmp.template tail<3>();
736 Matrix<T, 3, 1> p = delta.template head<3>();
737
738 /* Block-wise jacobian (additive position, local rotation): inverse of the jacobian of exp()
739 * J_exp = [A, B; 0, C] with A = Jl(theta), B = Q(rho, theta) - [p]x * Jl(theta), C = Jr(theta)
740 * J_log = [A.inv, -A.inv * B * C.inv; 0, C.inv]
741 */
742 Matrix<T, 3, 3> Jl_inv = jac_SO3_left_inv(theta);
743 Matrix<T, 3, 3> Jr_inv = jac_SO3_right_inv(theta);
744 Matrix<T, 3, 3> B = Q_helper(rho, theta) - skew(p) * jac_SO3_left(theta);
745
746 J_tau_delta.setZero();
747 J_tau_delta.template topLeftCorner<3, 3>() = Jl_inv;
748 J_tau_delta.template topRightCorner<3, 3>() = -Jl_inv * B * Jr_inv;
749 J_tau_delta.template bottomRightCorner<3, 3>() = Jr_inv;
750
751 tau = tau_tmp;
752}
753
756inline void log(const VectorComposite& delta, VectorComposite& tau)
757{
758 // temporaries: tangent and manifold 'O' have different sizes (tau and delta may be the same object)
759 Vector3d rho, theta;
760 log(delta.at('P'), delta.at('O'), rho, theta);
761 tau['P'] = rho;
762 tau['O'] = theta;
763}
764
768{
769 VectorComposite tau;
770 log(delta, tau);
771 return tau;
772}
773
775// PLUS (block-wise)
776
779template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
780inline void plus(const MatrixBase<D1>& p1,
781 const QuaternionBase<D2>& q1,
782 const MatrixBase<D4>& p2,
783 const MatrixBase<D5>& o2,
784 MatrixBase<D7>& plus_p,
785 QuaternionBase<D8>& plus_q)
786{
791
792 plus_p = p1 + p2;
793 plus_q = q1 * exp_q(o2);
794}
795
798template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
799inline void plus(const MatrixBase<D1>& p1,
800 const MatrixBase<D2>& q1,
801 const MatrixBase<D4>& p2,
802 const MatrixBase<D5>& o2,
803 MatrixBase<D7>& plus_p,
804 MatrixBase<D8>& plus_q)
805{
808
809 Map<const Quaternion<typename D2::Scalar>> q1m(&q1(0));
810 Map<Quaternion<typename D8::Scalar>> plus_qm(&plus_q(0));
811
812 plus(p1, q1m, p2, o2, plus_p, plus_qm);
813}
814
817template <typename D1, typename D2, typename D3>
818inline void plus(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2, MatrixBase<D3>& d_plus)
819{
823
824 Map<const Matrix<typename D1::Scalar, 3, 1>> p1(&d1(0));
825 Map<const Quaternion<typename D1::Scalar>> q1(&d1(3));
826 Map<const Matrix<typename D2::Scalar, 3, 1>> p2(&d2(0));
827 Map<const Matrix<typename D2::Scalar, 3, 1>> o2(&d2(3));
828 Map<Matrix<typename D3::Scalar, 3, 1>> p_p(&d_plus(0));
829 Map<Quaternion<typename D3::Scalar>> q_p(&d_plus(3));
830
831 plus(p1, q1, p2, o2, p_p, q_p);
832}
833
836template <typename D1, typename D2>
837inline Matrix<typename D1::Scalar, 7, 1> plus(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2)
838{
839 Matrix<typename D1::Scalar, 7, 1> d_plus;
840 plus(d1, d2, d_plus);
841 return d_plus;
842}
843
846inline void plus(const VectorComposite& x, const VectorComposite& tau, VectorComposite& res)
847{
848 res['P'].resize(3);
849 res['O'].resize(4);
850 plus(x.at('P'), x.at('O'), tau.at('P'), tau.at('O'), res['P'], res['O']);
851}
852
856{
857 VectorComposite res;
858 plus(x, tau, res);
859 return res;
860}
861
863// MINUS (block-wise)
864
867template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
868inline void minus(const MatrixBase<D1>& p1,
869 const QuaternionBase<D2>& q1,
870 const MatrixBase<D4>& p2,
871 const QuaternionBase<D5>& q2,
872 MatrixBase<D7>& pd,
873 MatrixBase<D8>& od)
874{
879
880 pd = p2 - p1;
881 od = log_q(q1.conjugate() * q2);
882}
883
887template <typename D1, typename D2, typename D4, typename D5, typename D6, typename D7, typename D8, typename D9>
888inline void minus(const MatrixBase<D1>& p1,
889 const QuaternionBase<D2>& q1,
890 const MatrixBase<D4>& p2,
891 const QuaternionBase<D5>& q2,
892 MatrixBase<D6>& pd,
893 MatrixBase<D7>& od,
894 MatrixBase<D8>& J_do_q1,
895 MatrixBase<D9>& J_do_q2)
896{
897 minus(p1, q1, p2, q2, pd, od);
898
899 J_do_q1 = -jac_SO3_left_inv(od);
900 J_do_q2 = jac_SO3_right_inv(od);
901}
902
905template <typename D1, typename D2, typename D4, typename D5, typename D7, typename D8>
906inline void minus(const MatrixBase<D1>& p1,
907 const MatrixBase<D2>& q1,
908 const MatrixBase<D4>& p2,
909 const MatrixBase<D5>& q2,
910 MatrixBase<D7>& pd,
911 MatrixBase<D8>& od)
912{
915
916 Map<const Quaternion<typename D2::Scalar>> mq1(&q1(0));
917 Map<const Quaternion<typename D5::Scalar>> mq2(&q2(0));
918
919 minus(p1, mq1, p2, mq2, pd, od);
920}
921
924template <typename D1, typename D2, typename D3>
925inline void minus(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2, MatrixBase<D3>& err)
926{
930
931 Map<const Matrix<typename D1::Scalar, 3, 1>> p1(&d1(0));
932 Map<const Quaternion<typename D1::Scalar>> q1(&d1(3));
933 Map<const Matrix<typename D2::Scalar, 3, 1>> p2(&d2(0));
934 Map<const Quaternion<typename D2::Scalar>> q2(&d2(3));
935 Map<Matrix<typename D3::Scalar, 3, 1>> pd(&err(0));
936 Map<Matrix<typename D3::Scalar, 3, 1>> od(&err(3));
937
938 minus(p1, q1, p2, q2, pd, od);
939}
940
943template <typename D1, typename D2, typename D3, typename D4, typename D5>
944inline void minus(const MatrixBase<D1>& d1,
945 const MatrixBase<D2>& d2,
946 MatrixBase<D3>& dif,
947 MatrixBase<D4>& J_diff_d1,
948 MatrixBase<D5>& J_diff_d2)
949{
955
956 Map<const Matrix<typename D1::Scalar, 3, 1>> p1(&d1(0));
957 Map<const Quaternion<typename D1::Scalar>> q1(&d1(3));
958 Map<const Matrix<typename D2::Scalar, 3, 1>> p2(&d2(0));
959 Map<const Quaternion<typename D2::Scalar>> q2(&d2(3));
960 Map<Matrix<typename D3::Scalar, 3, 1>> pd(&dif(0));
961 Map<Matrix<typename D3::Scalar, 3, 1>> od(&dif(3));
962
963 Matrix<typename D4::Scalar, 3, 3> J_do_q1, J_do_q2;
964
965 minus(p1, q1, p2, q2, pd, od, J_do_q1, J_do_q2);
966
967 /* d = minus(d1, d2) is
968 * pd = p2 - p1
969 * od = Log(q1.conj * q2)
970 *
971 * With trivial Jacobians for p and dv, and:
972 * J_do_q1 = - J_l_inv(theta)
973 * J_do_q2 = J_r_inv(theta)
974 */
975 J_diff_d1 = -Matrix<typename D4::Scalar, 6, 6>::Identity(); // d(p2 - p1) / d(p1) = - Identity
976 J_diff_d1.block(3, 3, 3, 3) = J_do_q1; // d(R1.tr*R2) / d(R1) = - J_l_inv(theta)
977
978 J_diff_d2.setIdentity(6, 6); // d(R1.tr*R2) / d(R2) = Identity
979 J_diff_d2.block(3, 3, 3, 3) = J_do_q2; // d(R1.tr*R2) / d(R1) = J_r_inv(theta)
980}
981
984template <typename D1, typename D2>
985inline Matrix<typename D1::Scalar, 6, 1> minus(const MatrixBase<D1>& d1, const MatrixBase<D2>& d2)
986{
987 Matrix<typename D1::Scalar, 6, 1> ret;
988 minus(d1, d2, ret);
989 return ret;
990}
991
994inline void minus(const VectorComposite& x1, const VectorComposite& x2, VectorComposite& res)
995{
996 // temporaries: tangent and manifold 'O' have different sizes (res and x1 or x2 may be the same object)
997 Vector3d pd, od;
998 minus(x1.at('P'), x1.at('O'), x2.at('P'), x2.at('O'), pd, od);
999 res['P'] = pd;
1000 res['O'] = od;
1001}
1002
1006{
1007 VectorComposite res;
1008 minus(x1, x2, res);
1009 return res;
1010}
1011
1013// INTERPOLATE
1014
1017template <typename D1, typename D2, typename D3>
1018inline void interpolate(const MatrixBase<D1>& d1,
1019 const MatrixBase<D2>& d2,
1020 const typename D1::Scalar& t,
1021 MatrixBase<D3>& ret)
1022{
1026
1027 Matrix<typename D1::Scalar, 7, 1> dd = between(d1, d2);
1028
1029 Matrix<typename D1::Scalar, 6, 1> tau = t * log(dd);
1030
1031 ret = compose(d1, exp(tau));
1032}
1033
1036template <typename D1, typename D2>
1037inline Matrix<typename D1::Scalar, 7, 1> interpolate(const MatrixBase<D1>& d1,
1038 const MatrixBase<D2>& d2,
1039 const typename D1::Scalar& t)
1040{
1041 Matrix<typename D1::Scalar, 7, 1> ret;
1042 interpolate(d1, d2, t, ret);
1043 return ret;
1044}
1045
1048inline void interpolate(const VectorComposite& x1, const VectorComposite& x2, const double& t, VectorComposite& res)
1049{
1050 VectorComposite tau = log(between(x1, x2));
1051 tau['P'] *= t;
1052 tau['O'] *= t;
1053 compose(x1, exp(tau), res);
1054}
1055
1058inline VectorComposite interpolate(const VectorComposite& x1, const VectorComposite& x2, const double& t)
1059{
1060 VectorComposite res;
1061 interpolate(x1, x2, t, res);
1062 return res;
1063}
1064
1066// HOMOGENEOUS MATRIX
1067
1070template <typename D1, typename D2>
1071inline void toSE3(const MatrixBase<D1>& pose, MatrixBase<D2>& SE3)
1072{
1075
1076 typedef typename D1::Scalar T;
1077
1078 SE3.template block<3, 1>(0, 3) = pose.template head<3>();
1079 SE3.template block<3, 3>(0, 0) = q2R(pose.template tail<4>());
1080 SE3.template block<1, 3>(3, 0).setZero();
1081 SE3(3, 3) = (T)1.0;
1082}
1083
1086template <typename D>
1087inline Matrix<typename D::Scalar, 4, 4> toSE3(const MatrixBase<D>& pose)
1088{
1089 Matrix<typename D::Scalar, 4, 4> SE3;
1090 toSE3(pose, SE3);
1091 return SE3;
1092}
1093
1094} // namespace SE3
1095} // namespace wolf
Composite of state vectors.
Definition: vector_composite.h:29
Namespace extending Eigen definitions.
Definition: wolf.h:91
void interpolate(const MatrixBase< D1 > &d1, const MatrixBase< D2 > &d2, const typename D1::Scalar &t, MatrixBase< D3 > &ret)
Interpolation of two deltas: d1 (+) exp(t * log(d1.inv (+) d2)) (full vectors)
Definition: SE3.h:1018
Matrix< typename D1::Scalar, 3, 3 > Q_helper(const MatrixBase< D1 > &v, const MatrixBase< D2 > &w)
Q matrix of the left Jacobian of SE(3), with v the linear and w the angular parts of the tangent vect...
Definition: SE3.h:85
Matrix< typename D::Scalar, 6, 6 > jac_SE3_right_inv(const MatrixBase< D > &tangent)
Inverse of the right Jacobian of SE(3), with tangent = [rho, theta].
Definition: SE3.h:172
void identity(MatrixBase< D1 > &p, QuaternionBase< D2 > &q)
Identity delta (by parts, with the quaternion as Eigen::QuaternionBase)
Definition: SE3.h:183
void plus(const MatrixBase< D1 > &p1, const QuaternionBase< D2 > &q1, const MatrixBase< D4 > &p2, const MatrixBase< D5 > &o2, MatrixBase< D7 > &plus_p, QuaternionBase< D8 > &plus_q)
Block-wise plus: [p1 + dp, q1 * exp_q(do)] (by parts, with the quaternion as Eigen::QuaternionBase)
Definition: SE3.h:780
void log(const MatrixBase< D1 > &p, const QuaternionBase< D2 > &q, MatrixBase< D3 > &rho, MatrixBase< D4 > &theta)
Logarithmic map: from the delta to the tangent vector [rho, theta] (by parts, with the quaternion as ...
Definition: SE3.h:671
Matrix< typename D::Scalar, 6, 6 > jac_SE3_right(const MatrixBase< D > &tangent)
Right Jacobian of SE(3), with tangent = [rho, theta].
Definition: SE3.h:143
void minus(const MatrixBase< D1 > &p1, const QuaternionBase< D2 > &q1, const MatrixBase< D4 > &p2, const QuaternionBase< D5 > &q2, MatrixBase< D7 > &pd, MatrixBase< D8 > &od)
Block-wise minus: [p2 - p1, log_q(q1.conj * q2)] (by parts, with the quaternion as Eigen::QuaternionB...
Definition: SE3.h:868
void compose(const MatrixBase< D1 > &p1, const QuaternionBase< D2 > &q1, const MatrixBase< D4 > &p2, const QuaternionBase< D5 > &q2, MatrixBase< D7 > &pc, QuaternionBase< D8 > &qc)
Composition of two deltas: d1 (+) d2 (by parts, with the quaternion as Eigen::QuaternionBase)
Definition: SE3.h:322
VectorComposite identityComposite()
Returns the identity delta, as a VectorComposite with keys 'P' and 'O'.
Definition: SE3.h:236
Matrix< typename D::Scalar, 6, 6 > jac_SE3_left_inv(const MatrixBase< D > &tangent)
Inverse of the left Jacobian of SE(3), with tangent = [rho, theta].
Definition: SE3.h:151
void exp(const MatrixBase< D1 > &rho, const MatrixBase< D2 > &theta, MatrixBase< D3 > &p, QuaternionBase< D4 > &q)
Exponential map: from the tangent vector [rho, theta] to the delta (by parts, with the quaternion as ...
Definition: SE3.h:563
void toSE3(const MatrixBase< D1 > &pose, MatrixBase< D2 > &SE3)
Homogeneous 4x4 matrix of a pose.
Definition: SE3.h:1071
void inverse(const MatrixBase< D1 > &p, const QuaternionBase< D2 > &q, MatrixBase< D4 > &ip, QuaternionBase< D5 > &iq)
Inverse delta (by parts, with the quaternion as Eigen::QuaternionBase)
Definition: SE3.h:249
Matrix< typename D::Scalar, 6, 6 > jac_SE3_left(const MatrixBase< D > &tangent)
Left Jacobian of SE(3), with tangent = [rho, theta].
Definition: SE3.h:122
void between(const MatrixBase< D1 > &p1, const QuaternionBase< D2 > &q1, const MatrixBase< D4 > &p2, const QuaternionBase< D5 > &q2, MatrixBase< D7 > &p12, QuaternionBase< D8 > &q12)
Delta between two deltas: d1.inv (+) d2 (by parts, with the quaternion as Eigen::QuaternionBase)
Definition: SE3.h:473
Jet< T, N > sin(const Jet< T, N > &f)
Definition: jet.h:641
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:617
Jet< T, N > cos(const Jet< T, N > &f)
Definition: jet.h:626
Definition: capture_base.h:23
Eigen::Matrix< typename Derived::Scalar, 3, 3 > q2R(const Eigen::QuaternionBase< Derived > &_q)
Quaternion to rotation matrix conversion.
Definition: rotations.h:108
Eigen::Matrix< typename Derived::Scalar, 3, 3 > jac_SO3_right_inv(const Eigen::MatrixBase< Derived > &_theta)
Computes Jrinv (inverse of Right Jacobian which corresponds to the jacobian of log) Right Jacobian fo...
Definition: rotations.h:472
Eigen::Quaternion< typename Derived::Scalar > exp_q(const Eigen::MatrixBase< Derived > &_v)
Quaternion exponential map.
Definition: rotations.h:267
Eigen::Matrix< typename Derived::Scalar, 3, 3 > jac_SO3_left(const Eigen::MatrixBase< Derived > &_theta)
Computes Jl (Left Jacobian) Left Jacobian for exp map in SO(3) - equation (10.86) and following equat...
Definition: rotations.h:506
Eigen::Matrix< typename Derived::Scalar, 3, 3 > jac_SO3_right(const Eigen::MatrixBase< Derived > &_theta)
Computes Jr (Right Jacobian) Right Jacobian for exp map in SO(3) - equation (10.86) and following equ...
Definition: rotations.h:432
Eigen::Matrix< typename Derived::Scalar, 3, 1 > log_q(const Eigen::QuaternionBase< Derived > &_q)
Quaternion logarithmic map.
Definition: rotations.h:296
Eigen::Matrix< typename Derived::Scalar, 3, 3 > jac_SO3_left_inv(const Eigen::MatrixBase< Derived > &_theta)
Computes Jl_inv (inverse of Left Jacobian which corresponds to the jacobian of log) Left Jacobian for...
Definition: rotations.h:545
Eigen::Matrix< typename Derived::Scalar, 3, 3 > skew(const Eigen::MatrixBase< Derived > &_v)
Skew symmetric matrix.
Definition: rotations.h:69
static void check(const Eigen::MatrixBase< T > &t)
Asserts the matrix size dynamically or statically.
Definition: eigen_assert.h:103